<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\common\library\wechat;

use cores\exception\BaseException;

/**
 * 链接录入接口
 * Class wechat
 * @package app\library
 */
class Link extends WxBase
{

     /**
     * 接口

     * @throws BaseException
     */
    public function getLinkLong(array $params=[])
    {
        // 微信接口url
        $accessToken = $this->getAccessTokenLink();
        $apiUrl = "https://api.weixin.qq.com/wxa/generate_urllink?access_token={$accessToken}";

        // 执行请求
        $result = $this->post($apiUrl, $this->jsonEncode($params));
   
        // 记录日志
        log_record(['name' => '微信小程序-短链接录入接口', 'url' => $apiUrl, 'params' => $params, 'result' => $result]);
        // 返回结果
        $response = json_decode($result,true);
      
        if (!isset($response['errcode'])) {
            $this->error = 'not found errcode';
        
                  throwError($this->error);
        }
        if ($response['errcode'] != 0) {
            $this->error = $response['errmsg'];
           throwError($this->error);
        }
        return $response;
    }
    /**
     * 接口

     * @throws BaseException
     */
    public function getLink(array $params=[])
    {
        // 微信接口url
        $accessToken = $this->getAccessTokenLink();
       // $apiUrl = "https://api.weixin.qq.com/wxa/generate_urllink?access_token={$accessToken}";

        //短连接
        $apiUrl = "https://api.weixin.qq.com/wxa/genwxashortlink?access_token={$accessToken}";
        // 执行请求
        $result = $this->post($apiUrl, $this->jsonEncode($params));
   
        // 记录日志
        log_record(['name' => '微信小程序-短链接录入接口', 'url' => $apiUrl, 'params' => $params, 'result' => $result]);
        // 返回结果
        $response = json_decode($result,true);
      
        if (!isset($response['errcode'])) {
            $this->error = 'not found errcode';
        
                  throwError($this->error);
        }
        if ($response['errcode'] != 0) {
            $this->error = $response['errmsg'];
           throwError($this->error);
        }
        return $response;
    }


}